package com.yunji.base.task;

import com.yunji.base.event.GuavaEventBus;
import com.yunji.common.enums.common.NoticeTypes;
import com.yunji.common.event.notice.InfraredWarmNoticeEvent;
import com.yunji.common.event.notice.LowBattleNoticeEvent;
import com.yunji.common.model.domain.NoticeImeiDO;
import com.yunji.common.model.dto.LoginInfo;
import com.yunji.system.service.NoticeImeiService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component("noticeTask")
public class NoticeTask {

    @Resource
    private  NoticeImeiService noticeImeiService;

    @Transactional
    public void noticeTask() {
        // 查找是否有需要消费的通知
        List<NoticeImeiDO> list = noticeImeiService.selectNoConsume();

        List<String> infraredWarmList = new ArrayList<>();
        List<String> lowBattleList = new ArrayList<>();

        for (NoticeImeiDO noticeImeiDO : list) {
            if (NoticeTypes.ABNORMAL_DEVICE_WARM.getCode().equals(noticeImeiDO.getNoticeType())) {
                infraredWarmList.add(noticeImeiDO.getImei());
            }else {
                lowBattleList.add(noticeImeiDO.getImei());
            }
        }

        GuavaEventBus.post(new InfraredWarmNoticeEvent(new LoginInfo(), infraredWarmList));
        GuavaEventBus.post(new LowBattleNoticeEvent(new LoginInfo(), lowBattleList));

        // 结束消费
        noticeImeiService.consumeNotice();

    }
}
